//
//  ViewController.m
//  WebView&&WKWebView
//
//  Created by wanglei on 17/1/18.
//  Copyright © 2017年 wanglei. All rights reserved.
//

#import "h5ViewController.h"

@interface h5ViewController ()<UIWebViewDelegate>

@property(nonatomic,strong)UIWebView *webview;
@property(nonatomic,strong)UIButton *barButtonBack;
@property(nonatomic,strong)UIButton *barButtonForward;

@end

@implementation h5ViewController



- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.navigationController setNavigationBarHidden:NO animated: YES];
    //    // 1.创建webview，并设置大小，"20"为状态栏高度
    //    CGFloat width = self.view.frame.size.width;
    //    CGFloat height = self.view.frame.size.height - 20;
    //    UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 20, width, height)];
    //    self.webview = webView;
    UIButton *btnBack = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 60, 40)];
    [btnBack setTitle:@"返回" forState:UIControlStateNormal];
    btnBack.backgroundColor = [UIColor yellowColor];
    UIButton *bthForward = [[UIButton alloc]initWithFrame:CGRectMake(80, 0, 60, 40)];
    [bthForward setTitle:@"前进" forState:UIControlStateNormal];
    bthForward.backgroundColor = [UIColor redColor];
    self.barButtonBack = btnBack;
    self.barButtonForward = bthForward;
    [self.view addSubview:_barButtonForward];
    [self.view addSubview:_barButtonBack];
    [_barButtonBack addTarget:self action:@selector(clickGoBackBtn) forControlEvents:UIControlEventTouchDragInside];
    [_barButtonForward addTarget:self action:@selector(clickGoForwardBtn) forControlEvents:UIControlEventTouchDragInside];
    [self initWebView];
}

- (void)initWebView{
    // 1.创建webview，并设置大小，"20"为状态栏高度
    CGFloat width = self.view.frame.size.width;
    CGFloat height = self.view.frame.size.height;
    UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0,0,width,height)];
    // 2.创建URL
    NSURL *url = [NSURL URLWithString:@"http://www.baidu.com"];
    // 3.创建Request
    NSURLRequest *request =[NSURLRequest requestWithURL:url];
    // 4.加载网页
    [webView loadRequest:request];
    // 5.最后将webView添加到界面
    [self.view addSubview:webView];
    self.webview = webView;
    _webview.delegate = self;
}
#pragma mark 设置前进后退按钮状态
-(void)setBarButtonStatus{
    if (_webview.canGoBack) {
        _barButtonBack.enabled = YES;
    }else{
        _barButtonBack.enabled = NO;
    }
    if(_webview.canGoForward){
        _barButtonForward.enabled = YES;
    }else{
        _barButtonForward.enabled = NO;
    }
}

#pragma mark - UIWebViewDelegate代理方法
#pragma mark 开始加载
//是否允许加载网页，也可获取js要打开的url，通过截取此url可与js交互
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request
 navigationType:(UIWebViewNavigationType)navigationType
{
//    if(navigationType == UIWebViewNavigationTypeBackForward){
//    self.webview.canGoBack ? [self.webview goBack]:[self.navigationController popViewControllerAnimated:YES];
//    }
    //截取URL，这里可以和JS进行交互，但这里没有写，因为会涉及到JS的一些知识，增加复杂性
    NSString *urlString = [request.URL absoluteString];
    urlString = [urlString stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSArray *urlComps = [urlString componentsSeparatedByString:@"://"];
    NSLog(@"urlString=%@---urlComps=%@",urlString,urlComps);
    return YES;
}
//开始加载网页
- (void)webViewDidStartLoad:(UIWebView *)webView{
    //显示网络请求加载,第三方框架：(MBProgressHUD）
    [UIApplication sharedApplication].networkActivityIndicatorVisible = true;
}
//网页加载完成
- (void)webViewDidFinishLoad:(UIWebView *)webView{
    //隐藏网络请求加载图标
    [UIApplication sharedApplication].networkActivityIndicatorVisible = false;
    [self setBarButtonStatus];
    //取得html内容
    NSLog(@"document.title = %@",[self.webview stringByEvaluatingJavaScriptFromString:@"document.title"]);
}
//网页加载错误
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"系统提示" message:@"网络连接发生错误!" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
    [alert show];
}

/**
 UIWebView加载网络请求
 */
- (void)simpleUIWebViewTest {
    // 2.创建URL
    NSURL *url = [NSURL URLWithString:@"http://www.baidu.com"];
    // 3.创建Request
    NSURLRequest *request =[NSURLRequest requestWithURL:url];
    // 4.加载网页
    [self.webview loadRequest:request];
    // 5.最后将webView添加到界面
    [self.view addSubview:_webview];
}


/**
 UIWebView加载HTML5字符串的操作
 */

- (void)loadLocalHTMLFileToUIWebView{
    // 获取本地html文件文件路径
    NSString *localHTMLPageName = @"myPage";
    NSString *path = [[NSBundle mainBundle] pathForResource:localHTMLPageName ofType:@"html"];
    // 从html文件中读取html字符串
    NSString *htmlString = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:NULL];
    // 加载本地HTML字符串
    [self.webview loadHTMLString:htmlString baseURL:[[NSBundle mainBundle] bundleURL]];
    [self.view addSubview:_webview];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
